from time import sleep, time
from lxml import etree
from selenium.webdriver import Chrome
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

"""
陈雨杰爬取欧洲股票数据
"""
opt = Options()
opt.add_argument("--headless")
opt.add_argument("--disable-gpu")
edge =Chrome(options=opt)
url = 'http://quote.eastmoney.com/center/gridlist.html#global_euro'
edge.get(url)
sleep(10)
page = edge.page_source
html = etree.HTML(page)


for i in range(0,2): #循环遍历查询各个的数据

    table = edge.find_elements(by=By.XPATH, value="//table[@id='table_wrapper-table']/tbody/tr") #
    for tr in table:
        # 提取文本
        txt = tr.text
        arr = txt.split(' ')  # 提取插入列表

        # 国家名称
        country_name = arr[1]
        # 最新价
        latest_price = arr[2]
        # 涨跌额
        rising_falling_amounts = arr[3]
        # 涨跌幅
        percentage_change = arr[4]
        # 开盘价
        opening_price = arr[5]
        # 最高价
        maximum_price = arr[6]
        # 最低价
        bottom_price = arr[7]
        # 昨收价
        closing_price = arr[8]
        # 振幅
        amplitude = arr[9]

        print(f"{country_name},"
              f"{latest_price},"
              f"{rising_falling_amounts},"
              f"{percentage_change},"
              f"{opening_price},"
              f"{maximum_price},"
              f"{bottom_price},"
              f"{closing_price},"
              f"{amplitude}")

    next = edge.find_elements(by=By.XPATH, value='// *[ @ id = "main-table_paginate"] / a[2]')[0] #下一页按钮打印下一页
    next.click()
    sleep(3)